package com.common.map;

import io.netty.channel.Channel;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class NettyChannelMap {

    private NettyChannelMap() {

    }

    private volatile static Map<String,Channel> map;

    public static Map<String,Channel> getInstance(){
        if(map == null){
            synchronized(Map.class){
                if(map == null){
                    map = new ConcurrentHashMap<>();
                }
            }
        }
        return map;
    }
    public static void add(String clientId,Channel channel){
        if(!map.containsKey(clientId)){
            map.put(clientId,channel);
        }
    }
    public static Channel get(String clientId){
        return map.get(clientId);
    }
    public static void remove(Channel channel){
        for (Map.Entry entry:map.entrySet()){
            if (entry.getValue()==channel){
                map.remove(entry.getKey());
            }
        }
    }


}
